import { IShaderBase } from "../base_frame/IShaderBase";
import { ShaderBase } from "../base_frame/ShaderBase";
import { ShaderTool } from "../base_frame/ShaderTool";
import { BaseCamera } from "../base_frame/BaseCamera";

/**
 * 
 * !! TODO 可以做个每个顶点不同颜色的定义来绘制，而不是使用共享顶点的方式   
 */
export class DrawElement extends ShaderBase implements IShaderBase {
    
    protected readonly VS: string =
        "attribute vec4 a_Position;\n" +
        "attribute vec4 a_Color;\n" +
        "uniform mat4 u_MvpMatrix;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){\n" +
        "   v_Color = a_Color;\n" +
        "   gl_Position = u_MvpMatrix * a_Position;\n" +
        "}\n";

    protected readonly FS: string =
        "precision mediump float;\n" +
        "varying vec4 v_Color;\n" +
        "void main(){\n" +
        "   gl_FragColor = v_Color;\n" +
        "}\n";

    protected a_Position: number;
    protected a_Color: number;
    private _indices:number;


    init(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        if(! initShaders(gl, this.VS, this.FS))return console.error("init shader fail");
        this.initShaderRef(gl);
        this.initBuffer(gl);
        this.setShaderValue(gl);
    }
    setShaderValue(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        
    }

    initBuffer(gl: WebGL2RenderingContext | WebGLRenderingContext) :number{
        let vertexBuffer = gl.createBuffer();
        let colorBuffer = gl.createBuffer();
        let indiceBuffer = gl.createBuffer();
        if(! vertexBuffer || ! colorBuffer || !indiceBuffer)  {console.error("init buffer fail"); return 0};
        let cubeVertices = new Float32Array([
            -1.0,1.0,1.0,   1.0,0.0,0.0, //0
            -1.0,-1.0,1.0,  0.0,0.0,0.0, //1
            1.0,1.0,1.0,  0.0,1.0,0.0, //2
            1.0,-1.0,1.0,  0.0,0.0,1.0,// 3

            -1.0,1.0,-1.0,   0.5,0.5,0.0,//4
            -1.0,-1.0,-1.0,  0.0,1.0,1.0,//5
            1.0,1.0,-1.0,  1.0,1.0,0.0,//6
            1.0,-1.0,-1.0,  1.0,1.0,1.0,//7
        ]);

        let indices = new Uint8Array([
            0,1,2, 1,2,3,  // 前
            4,5,6, 5,6,7,  // 后

            0,2,4, 2,4,6,
            1,3,5, 3,5,7,
            0,1,4, 5,1,4,
            2,3,6, 6,3,7
        ]);
        let size = cubeVertices.BYTES_PER_ELEMENT;
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, cubeVertices, gl.STATIC_DRAW);
        gl.vertexAttribPointer(this.a_Position, 3, gl.FLOAT, false, size * 6, 0);
        gl.enableVertexAttribArray(this.a_Position);

        gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
        gl.bufferData(gl.ARRAY_BUFFER, cubeVertices, gl.STATIC_DRAW);
        gl.vertexAttribPointer(this.a_Color, 3, gl.FLOAT, false, size * 6, size * 3);
        gl.enableVertexAttribArray(this.a_Color);

        size = indices.BYTES_PER_ELEMENT;
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indiceBuffer);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
        this._indices = indices.length;
        return indices.length;
    }

    initShaderRef(gl: WebGL2RenderingContext | WebGLRenderingContext) {
        //@ts-ignore
        let program = gl.program;
        this.a_Position = gl.getAttribLocation(program, "a_Position");
        this.a_Color = gl.getAttribLocation(program, "a_Color");
    }

    inputBlur(text: string): void {
        if (text) {
            ShaderTool.setProperty(text, this);
        }
    }

    draw(gl: WebGL2RenderingContext | WebGLRenderingContext): void {
        gl.clearColor(0.0,0.0,0.0,1.0);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
        gl.polygonOffset(1.0,1.0);
        gl.drawElements(gl.TRIANGLES, this._indices, gl.UNSIGNED_BYTE, 0);
    }

}